/*
 * Copyright (C) 2010 TopCoder Inc., All Rights Reserved.
 */
package com.hercules.xre.protocol.testing.netty.http;

import com.hercules.xre.protocol.testing.EventHandler;
import com.hercules.xre.protocol.testing.Helper;
import com.hercules.xre.protocol.testing.netty.BaseProtocolTestingConnectionManager;
import com.hercules.xre.protocol.testing.netty.SSLEngineFactory;

import org.jboss.netty.channel.ChannelPipelineFactory;
/**
 * <p>
 * XRE connection manager that is responsible for handling communication trough HTTP.
 * </p>
 * <p>
 * <strong>Thread-safety:</strong> This class isn't thread safe as it's parent isn't thread safe.
 * </p>
 * @author TCSDESIGNER, TCSDEVELOPER
 * @version 1.0
*/
public class HTTPProtocolTestingConnectionManager extends BaseProtocolTestingConnectionManager {
    /**
     * <p>
     * Name of HTTP Request parameter that will contain XRE events. Can't be null,
     * can't be empty, doesn't change after initialization.
     * </p>
    */
    private final String  requestParameterName;
    /**
     * <p>
     * Creates new instance.
     * </p>
     * @param sslEngineFactory ssl engine factory, can be null (if null no encryption will be used).
     * @param requestParameterName name of request parameter that will hold serialized
     *        XREEvents, can't be null, can't be empty
     * @param bindAddress address to which application should be bound, if null is
     *        passed then application will bind on all available interfaces
     * @param port port number on which connection manager should be listening.
     *        A valid port value is between 1 and 65535.
     * @throws IllegalArgumentException when any of passed parameters is invalid
    */
    public HTTPProtocolTestingConnectionManager(int port, String bindAddress,
        SSLEngineFactory sslEngineFactory, String requestParameterName) {
        super(Helper.checkPortAndLog("TCPProtocolTestingConnectionManager#ctor",
            new String[] {"port", "bindAddress", "sslEngineFactory"},
                new Object[] {port, bindAddress, sslEngineFactory}, LOGGER), bindAddress, sslEngineFactory);
        final String signature = "HTTPProtocolTestingConnectionManager#ctor";
        try {
            Helper.checkString(requestParameterName, "requestParameterName", signature, LOGGER);
            this.requestParameterName = requestParameterName;
        } finally {
            Helper.logExit(signature, null, LOGGER);
        }
    }

    /**
     * <p>
     * Creates new instance of pipeline factory that should be used for handling HTTP communication.
     * </p>
     * @param eventHandler event handler that should be used to inform application about
     *        communication events, can't be null
     * @return HTTPConnectionChannelPipelineFactory instance.
     * @throws IllegalArgumentException when any of passed parameters is invalid
     */
    protected ChannelPipelineFactory getPipelineFactory(EventHandler eventHandler) {
        final String signature = "HTTPProtocolTestingConnectionManager#getPipelineFactory";
        Helper.logEnter(signature, new String[] {"eventHandler"}, new Object[] {eventHandler}, LOGGER);
        HTTPConnectionChannelPipelineFactory res = null;
        try {
            Helper.checkNull(eventHandler, "eventHandler", signature, LOGGER);
            res = new HTTPConnectionChannelPipelineFactory(this, eventHandler, requestParameterName);
        } finally {
            Helper.logExit(signature, res, LOGGER);
        }
        return res;
    }
}

